<?php
declare(strict_types = 1);
namespace app\admin\model;
use think\Config;
use think\facade\Db;
use think\Model;
class GoodsProp extends Model{
    public function getStatusAttr($value){
        if (\think\facade\Config::has('view.par_status')){
            $config = \think\facade\Config::get('view.par_status');
            return $config[$value];
        }
    }

    //查询
    public function searchKeywordsAttr($query,$v){
        if (''!==$v){
            $query->where('ecm_goods_prop.pname','like','%'.$v.'%');
        }
    }
    public function searchPvaluesAttr($query,$value){
        if (isset($value)  && '200'!=$value){
            foreach ($value as $v){
                $query->whereFindInSet('pvalues',$v);
            }
        }
//        if (isset($value) && is_scalar($value) && '200'!= $value){
//            $query->where('ecm_goods_prop.pvalues',strval($value));
//       }
//        if (isset($value) && is_array($value) && count($value) > 0){
//            $query->whereIn('ecm_goods_prop.pvalues',$value);
//        }
    }
    public static function text(){
        $model = self::table('ecm_goods_prop_value')->select();
        return $model;
    }
    public static function GoodsProp($data=[],$page=true,$query = []){
        $field = [
        'p.pid','p.pname','p.pvalues','p.status','v.prop_value','v.vid'
        ];

       //取值
        $model = self::field($field)
            ->alias('p')
            ->join(['ecm_goods_prop_value'=>'v'],'v.vid=p.pvalues','left')
            ->withSearch(array_keys($data),$data);

        //查询

//        if (!empty($data['vid'])){
//            foreach ($data['vid'] as $k=>$v){
//                $model->whereFindInSet('pvalues',$v);
//            }
//        }

        //分页
        if ($page){
            $list_rows= \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows'=>  $list_rows,
                'query'    => $query,
            ])->each(function($v,$k){
           $shuxing=Db::table('ecm_goods_prop_value')
               ->field('pid,prop_value,vid')
               ->where("vid in ({$v['pvalues']})")
               ->select();
           $arr=[];
           foreach($shuxing as $kk=>$vv){
               $arr[]=$vv['prop_value'];
               $a=implode(',',$arr);
           }
           $v['a']=$a;
           return $v;
       });
        }else{
            return $model->select();
        }

    }
}